<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>无限视差滚动</title>
    <style>
        * {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }
 
  body {
    overflow: hidden;
  }
 
  .item {
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;
    transition: 1s ease-in-out;
  }
 
  .item img {
    position: absolute;
    width: 100%;
    height: 100vh;
    object-fit: cover;
    transition: 1s ease-in-out;
  }
 
  .scroll-container {
    height: 100vh;
    position: relative;
  }
 
  .item.pre,
  .item.next {
    z-index: 1;
    height: 0;
  }
 
  .item.pre {
    top: 0;
  }
 
  .item.next {
    bottom: 0
  }
 
  .item.pre img {
    transform: translateY(-10%);
  }
 
  .item.next img {
    bottom: 0;
    transform: translateY(10%);
  }
 
  .scroll-up .pre,
  .scroll-down .next {
    height: 100%;
  }
 
  .scroll-up .pre img {
    transform: translateY(0);
  }
 
  .scroll-up .cur img {
    transform: translateY(10%);
  }
 
  .scroll-down .next img {
    transform: translateY(0);
  }
 
  .scroll-down .cur img {
    transform: translateY(-10%);
  }
    </style>
</head>
<body>
    <div class="scroll-container"></div>
    <script>
        // https://blog.csdn.net/Linxi_001/article/details/130846706
        const imgs = [
            'https://picsum.photos/1024/900?1',
            'https://picsum.photos/1024/900?2',
            'https://picsum.photos/1024/900?3',
            'https://picsum.photos/1024/900?4',
            'https://picsum.photos/1024/900?5',
        ];
        const container = document.querySelector('.scroll-container');
        let curIndex = 0;
        function getPrevIndex(){
            return curIndex===0?imgs.length-1:curIndex-1;
        }
        function getNextIndex(){
            return curIndex===imgs.length-1?0:curIndex+1;
        }
        function creatElement(i){
            const div = document.createElement('div');
            div.className = 'item'
            const img = document.createElement('img');
            img.src = imgs[i];
            div.appendChild(img);
            container.appendChild(div);
            return div;
        }
        function resetElements(){
            container.innerHTML='';
            const prevIndex = getPrevIndex();
            const nextIndex = getNextIndex();
            creatElement(prevIndex).classList.add('prev');
            creatElement(curIndex).classList.add('cur');
            creatElement(nextIndex).classList.add('next');
        }
        resetElements();
        let isAnimation = false;
        window.addEventListener('wheel',(e)=>{
            if(isAnimation||e.deltaY==0){
                return;
            }
            isAnimation = true;
            if(e.deltaY>0){
                curIndex = getNextIndex();
                container.classList.add('scroll-down');
            }else if(e.deltaY<0){
                curIndex = getPrevIndex();
                container.classList.add('scroll-up');
            }
        })
        container.addEventListener('transitionend',()=>{
            container.classList.remove('scroll-down');
            container.classList.remove('scroll-up');
            isAnimation = false;
            resetElements();
        })
    </script>
</body>
</html>